package com.erp.controller;


import com.erp.entity.Role;
import com.erp.entity.result.Result;
import com.erp.entity.result.ResultStatus;
import com.erp.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pyj
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/findRoleByName")
    public Result findRoleByName(String name) {
        return new Result(ResultStatus.SUCCESS, roleService.findRoleByName(name));
    }

    @GetMapping("/getAllRoles")
    public Result getAllRoles() {
        return new Result(ResultStatus.SUCCESS, roleService.getAllRoles());
    }
    @GetMapping("/getRoleByPage")
    public Result getRoleByPage(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        PageHelper.startPage(page,pageSize);
        List<Role> list = roleService.getAll();
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Result(ResultStatus.SUCCESS, pageInfo);
    }


    @PostMapping("/add")
    public Result insert(@RequestBody Role role) {
        if (roleService.add(role) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @GetMapping("/delete")
    public Result delete(@PathParam("id") Integer id) {
        if (roleService.delete(id) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Role role) {
        
        if (roleService.update(role) > 0) {
            return new Result(ResultStatus.SUCCESS, null);
        } else {
            return new Result(ResultStatus.BAD_REQUEST, null);
        }
    }
}

